package com.jnu.mydiary.ui.users;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.mydiary.MainActivity;
import com.jnu.mydiary.R;
import com.jnu.mydiary.databinding.FragmentUsersBinding;
import com.jnu.mydiary.ui.daka.NotificationsViewModel;
import com.jnu.mydiary.ui.shouye1.DataBank;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

public class UsersFragment extends Fragment {

    private UsersViewModel usersViewModel;
    private FragmentUsersBinding binding;

    private TextView textViewUserName;
    private TextView textViewUserTalk;
    private TextView textViewUserMail;
    private TextView textViewUserPhone;
    private ImageView imageView;
    private static Bitmap bitmap;
    private String talk;
    private String name;
    private String mail;
    private  String phone;

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==0){
            if(null==data)return;
            DataBank mDataBank=new DataBank(getActivity());
            textViewUserName=binding.getRoot().findViewById(R.id.textView_userName);
            textViewUserTalk=getView().findViewById(R.id.textView_user_talk);
            textViewUserMail=binding.getRoot().findViewById(R.id.textView_user_mail);
            textViewUserPhone=getView().findViewById(R.id.textView_user_Phone);

            name=textViewUserName.getText().toString();
            talk=textViewUserTalk.getText().toString();
            mail=textViewUserMail.getText().toString();
            phone=textViewUserPhone.getText().toString();
            User user=new User(name,talk,phone,mail);
            mDataBank.saveUser(user);
            name = data.getStringExtra("name");
            talk = data.getStringExtra("talk");
            mail = data.getStringExtra("mail");
            phone = data.getStringExtra("phone");

            User mUser=new User(name,talk,phone,mail);
            mDataBank.saveUser(mUser);

            textViewUserName.setText(name);
            textViewUserTalk.setText(talk);
            textViewUserMail.setText(mail);
            textViewUserPhone.setText(phone);
        }
        if (requestCode == 1&& null != data)
        {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {
                    MediaStore.Images.Media.DATA
            };
            Cursor cursor = UsersFragment.this.getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);

            cursor.close(); // String picturePath contains the path of selected Image }
            imageView = getView().findViewById(R.id.imageView_picture);
            bitmap= null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(UsersFragment.this.getContext().getContentResolver(),selectedImage);

            } catch (IOException e) {
                e.printStackTrace();
            }

            imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,100,100));
            //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {


        binding = FragmentUsersBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        usersViewModel =
                new ViewModelProvider(this).get(UsersViewModel.class);
        DataBank mDataBank=new DataBank(getActivity());
       User mUser=mDataBank.loadUser();
        textViewUserName=root.findViewById(R.id.textView_userName);
        textViewUserTalk=root.findViewById(R.id.textView_user_talk);
        textViewUserMail=root.findViewById(R.id.textView_user_mail);
        textViewUserPhone=root.findViewById(R.id.textView_user_Phone);
        if(mUser==null) {
            name = textViewUserName.getText().toString();
            talk = textViewUserTalk.getText().toString();
            mail = textViewUserMail.getText().toString();
            phone = textViewUserPhone.getText().toString();
            User user = new User(name, talk, phone, mail);
            mDataBank.saveUser(user);
        }
        if(mUser!=null) {
            name = mUser.getUserName();
            talk = mUser.getMark();
            phone = mUser.getPhone();
            mail = mUser.getMail();
        }

        if(name!=null) {
            textViewUserName.setText(name);
        }
        if(talk!=null) {
            textViewUserTalk.setText(talk);
        }
        if(mail!=null) {
            textViewUserMail.setText(mail);
        }
        if(phone!=null) {
            textViewUserPhone.setText(phone);
        }
        imageView = root.findViewById(R.id.imageView_picture);

        if(bitmap!=null){
            imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,100,100));
        }
        Button buttonEdit=root.findViewById(R.id.user_button_edit);
        buttonEdit.setOnClickListener(v -> {
            Intent intent=new Intent(UsersFragment.this.getContext(),EditUserActivity.class);
            intent.putExtra("type","user");
           intent.putExtra("now_day",0);
            startActivityForResult(intent,0);
        });
        Button buttonSelectPicture=root.findViewById(R.id.button_selectPicture);
        buttonSelectPicture.setOnClickListener(v->{
            Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 1);
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}